<?php
class NewsController extends AppController {
	var $name = 'News';
	var $helpers = array('Html','Ck');
	var $components = array('Init', 'Param','Image');

	function beforeFilter() {
		if (!$this->Session->read('admin')) {
			$this->redirect('/users/login');
		}
		else {
			$this->Init->startup($this);
			$this->Admin = $this->Init->init();

			$this->set('dashboard',0);
			$this->set('content',1);
			$this->set('products',0);
			$this->set('component',0);
			$this->set('system',0);

			$this->set('title','News Management');
			$this->set('sub_title', $this->Param->getAction($this->params));
  		}
	}

	function index(){
		$this->paginate = array(
			'News' => array(
				'limit' => 10,
				'order'	=> array('News.date desc')
			)
		);
		$this->set('news',$this->paginate('News'));
	}

	function edit(){
		$this->loadModel('News');
		App::import('Vendor', 'SpawEditor', array('file' => 'editor/spaw.inc.php'));
		$id = $this->Param->getID($this->params);

		if (isset($this->data)) {
			$this->data['News']['description_eng'] = trim(strip_tags($this->data['News']['description_eng']));
			$this->data['News']['content_eng'] = $this->data['News']['content_eng'];
			$this->data['News']['description_vie'] = trim(strip_tags($this->data['News']['description_vie']));
			$this->data['News']['content_vie'] = $this->data['News']['content_vie'];
			$this->data['News']['date'] = time();
			$this->data['News']['title_eng'] = trim(strip_tags($this->data['News']['title_eng']));
			$this->data['News']['title_vie'] = trim(strip_tags($this->data['News']['title_vie']));

			$this->data['News']['tag'] = strip_tags($this->data['News']['tag']);

			if(!empty($_FILES['upload']['name'])) {
				
				App::import('Component', 'Upload');
				$upload = new UploadComponent();
				$upload->setPath('news');
				$_error = $upload->checkImage($_FILES['upload']['size']);

				if(empty($_error)) {
					$_file = $upload->upload($_FILES['upload'], $id);
					$this->data['News']['image'] = $_file;
				}
				else {
					$this->Session->setFlash($_error);
					$this->redirect('/news');
				}
			}

			if ($this->News->save($this->data)) {
				$this->Session->setFlash('Done successfully!');
				$this->redirect('/news');
			}

		}
		else {
			if (isset($id) && ($id > 0)) {
				$this->data = $this->News->find('first',array(
					'conditions' => array('News.id' => $id)
				));
			}
		}
	}

	function del($id = null) {
		$id = $this->Param->getID($this->params);
		 $this->News->id = $id;
		 $this->News->delete();
		 $this->Session->setFlash('aaa');
		 $this->redirect('/news');
	}


}
?>